//
//  QLTabBarController.m
//  青旅O2O
//
//  Created by qlwl on 16/4/29.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLTabBarController.h"
#import "AppDelegate+QLDelegate.h"
#import "UIButton+Action.h"


@interface QLTabBarController ()
@property (nonatomic,assign) NSInteger sessionUnreadCount; //聊天
@property (nonatomic,assign) NSInteger systemUnreadCount; //通讯录

@property (nonatomic, weak) UINavigationController * circleVC ; //
@end

@implementation QLTabBarController

- (void)viewDidLoad {
    
//    HSTabBar *tabBar = [[HSTabBar alloc] init];
//    tabBar.tabBarDelegate = self;
//    [self setValue:tabBar forKeyPath:@"tabBar"];
    
    self.tabBar.tintColor=TabBarTintColor;
//    self.tabBar.barTintColor=[UIColor colorWithRed:0.91f green:0.35f blue:0.12f alpha:1.00f];;
    [super viewDidLoad];
    
    self.tabBar.tintColor=[UIColor colorWithRed:0.91f green:0.61f blue:0.00f alpha:1.00f];
    
    ((AppDelegate *)[UIApplication sharedApplication].delegate).tab = self;
    self.delegate = ((AppDelegate *)[UIApplication sharedApplication].delegate);
    //设置tabBar 隐藏
    //self.tabBar.hidden = YES;
    //设置分割线的颜色
    //[self.tabBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00] andRect:CGRectMake(0, 0, iphoneWidth, 1)]];
    //设置底部颜色
    //[self.tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00] andRect:CGRectMake(0, 0, iphoneWidth, 49)]];
    //[self.tabBar setSelectionIndicatorImage:[UIImage imageWithColor:[UIColor redColor] andRect:CGRectMake(0, 0, iphoneWidth/3, 49)]];
    //添加代理
   
    
    [self addChildVc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addChildVc
{
    [self addChildVCWithSBName:@"Home" title:@"首页" norImageName:@"iconItem0_" selectedImageName:@"iconItem0_"];
    [self addChildVCWithSBName:@"Dynamic" title:@"动态" norImageName:@"iconItem1" selectedImageName:@"iconItem1"];
    [self addChildVCWithSBName:@"News" title:@"消息" norImageName:@"iconItem3" selectedImageName:@"iconItem3"];
    [self addChildVCWithSBName:@"Mine" title:@"我的" norImageName:@"iconItem4" selectedImageName:@"iconItem4"];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
// 通过一个SB创建控制器
- (void)addChildVCWithSBName:(NSString *)sbName  title:(NSString *)title norImageName:(NSString *)norImageName selectedImageName:(NSString *)selectedImageName{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
    
    UINavigationController *nav = sb.instantiateInitialViewController;
    if ([sbName isEqualToString:@"Circle"]) {
        self.circleVC = nav;
        [self refreshCicleBadge];
    }
    [self addChildVCWithController:nav.topViewController title:title norImageName:norImageName selectedImageName:selectedImageName];
}

// 设置指定控制器的相关属性
- (void)addChildVCWithController:(UIViewController *)vc  title:(NSString *)title norImageName:(NSString *)norImageName selectedImageName:(NSString *)selectedImageName{
    vc.tabBarItem.title = title;
    //vc.navigationItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:norImageName];
    
    //vc.tabBarItem.selectedImage =  [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    
    [self addChildViewController:vc.navigationController];
    
    //        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
    //        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
}



#pragma mark - NIMSystemNotificationManagerDelegate
- (void)onSystemNotificationCountChanged:(NSInteger)unreadCount
{
    self.systemUnreadCount = unreadCount;
    [self refreshCicleBadge];
}


#pragma mark refresh cicleNum
- (void)refreshCicleBadge{
    UINavigationController * nav = self.circleVC;
    NSInteger badge = self.sessionUnreadCount + self.systemUnreadCount;
    nav.tabBarItem.badgeValue = badge ? @(badge).stringValue : nil;
}



@end

